package com.xbongbong.workorder.abstracts;

import com.alibaba.fastjson.JSONObject;
import com.xbongbong.help.log.InsertDataLogHelp;
import com.xbongbong.paas.business.rule.FormDataActiveRuleHelp;
import com.xbongbong.paas.data.service.RichTextService;
import com.xbongbong.paas.domain.entity.PaasFormDataEntity;
import com.xbongbong.paas.domain.entity.ext.PaasFormEntityExt;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.pojo.dto.ActiveRuleDTO;
import com.xbongbong.paas.pojo.dto.FormDataAddDTO;
import com.xbongbong.paas.pojo.vo.FormDataAddVO;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.BeanUtil;
import com.xbongbong.paas.util.FormDataUtil;
import com.xbongbong.parent.abstracts.AbstractParentAddFormDataService;
import com.xbongbong.parent.interfaces.FormDataService;
import com.xbongbong.parent.interfaces.FormDataWorkflowService;
import com.xbongbong.pro.formdata.pojo.vo.BeforeSaveVO;
import com.xbongbong.pro.formdata.pojo.vo.SaveVO;
import com.xbongbong.process.operate.ProcessFieldHelp;
import com.xbongbong.process.operate.ProcessStartHelp;
import com.xbongbong.saas.domain.entity.WorkOrderEntity;
import com.xbongbong.saas.domain.entity.ext.WorkOrderFormEntityExt;
import com.xbongbong.saas.enums.OperateModuleTypeEnum;
import com.xbongbong.paas.service.workflow.trigger.WorkflowFormDataOperationService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Map;
import java.util.Objects;

/**
 * 进销存抽象类，实现进销存中的公用方法
 * @author 吴峰
 * @date 2022/01/05 10:12
 */
@Component
public abstract class AbstractWorkOrderAddDataService extends AbstractParentAddFormDataService<WorkOrderFormEntityExt, WorkOrderEntity> implements FormDataService<WorkOrderFormEntityExt, WorkOrderEntity>, FormDataWorkflowService<WorkOrderFormEntityExt, WorkOrderEntity> {

    @Resource
    private ProcessStartHelp processStartHelp;
    @Resource
    private WorkflowFormDataOperationService workflowFormDataOperationService;
    @Resource
    private InsertDataLogHelp insertDataLogHelp;
    @Resource
    private FormDataActiveRuleHelp formDataActiveRuleHelp;
    @Resource
    private ProcessFieldHelp processFieldHelp;
    @Resource
    private RichTextService richTextService;

    /**
     * 保存富文本
     *
     * @param formDataAddDTO
     * @param paasFormDataEntity
     * @param richTextData 富文本数据
     * @throws XbbException
     */
    protected void saveRichText(FormDataAddDTO formDataAddDTO, PaasFormDataEntity paasFormDataEntity,Object richTextData) throws XbbException{
        richTextService.saveFormRichText(formDataAddDTO, paasFormDataEntity, richTextData);
    }

    /**
     * 移除富文本
     *
     * @param data Json数据
     * @return 富文本数据
     * @throws XbbException
     */
    protected Object removeRichText(JSONObject data) throws XbbException{
        return FormDataUtil.removeRichText(data);
    }

    @Override
    public boolean isEnableNewWorkflow(FormDataAddDTO formDataSaveDTO, BeforeSaveVO beforeSaveVO, WorkOrderEntity paasFormDataEntity) throws XbbException {
        return workflowFormDataOperationService.isEnableNewWorkflow(formDataSaveDTO, beforeSaveVO, paasFormDataEntity);
    }

    @Override
    public Integer startProcess(FormDataAddDTO formDataAddDTO, BeforeSaveVO beforeSaveVO) throws XbbException {
        return processStartHelp.startProcess(formDataAddDTO, beforeSaveVO);
    }

    /**
     * 保存日志
     * @param formDataAddDTO
     * @param paasFormDataEntity
     * @param logName 日志名称
     * @param name
     * @param operateModuleTypeEnum
     * @param saveVO 插入接口返回的数据，
     * @param beforeSaveVO 备用参数
     * @return void
     * @author 吴峰
     * @date 2021/2/5 10:47
     * @throws XbbException
     */
    protected void saveLogger(FormDataAddDTO formDataAddDTO, PaasFormDataEntity paasFormDataEntity, String logName, String name, OperateModuleTypeEnum operateModuleTypeEnum, SaveVO saveVO,
                              BeforeSaveVO beforeSaveVO) {
        insertDataLogHelp.saveLogger(formDataAddDTO, paasFormDataEntity, logName, name, operateModuleTypeEnum, saveVO, beforeSaveVO);
    }

    @Override
    public Map<String, FieldAttrEntity> processSetVisible(WorkOrderFormEntityExt paasFormEntityExt, FormDataAddDTO formDataSaveDTO, Map<String, FieldAttrEntity> explainMap) throws XbbException {
        return processFieldHelp.processSetVisible(paasFormEntityExt.getIsProcessForm(), paasFormEntityExt.getId(), paasFormEntityExt.getAppId(), formDataSaveDTO, explainMap);
    }

    @Override
    public Map<String, FieldAttrEntity> importProcessSetVisible(WorkOrderFormEntityExt paasFormEntityExt, FormDataAddDTO formDataSaveDTO, Map<String, FieldAttrEntity> explainMap) throws XbbException {
        return processFieldHelp.importProcessSetVisible(paasFormEntityExt.getIsProcessForm(), paasFormEntityExt.getId(), paasFormEntityExt.getAppId(), formDataSaveDTO, explainMap);
    }

    /**
     * 执行业务规则
     * @param formDataAddDTO
     * @param beforeSaveVO
     * @param newPaasFormDataEntity 数据实体
     * @param paasFormEntityExt 表单实体
     * @throws XbbException
     */
    @Override
    public void activeRule(FormDataAddDTO formDataAddDTO, BeforeSaveVO beforeSaveVO, PaasFormDataEntity newPaasFormDataEntity, PaasFormEntityExt paasFormEntityExt) throws XbbException{
        String businessRules = paasFormEntityExt.getBusinessRules();
        if (!Objects.equals(1, formDataAddDTO.getIsBusinessRule()) && Objects.equals("1", businessRules)) {
            ActiveRuleDTO activeRuleDTO = new ActiveRuleDTO();
            BeanUtil.copyProperties(formDataAddDTO, activeRuleDTO);
            activeRuleDTO.setDataId(newPaasFormDataEntity.getId());
            activeRuleDTO.setPaasFormDataEntity(newPaasFormDataEntity);
            activeRuleDTO.setExplainList(beforeSaveVO.getExplainList());
            formDataActiveRuleHelp.activeRule(activeRuleDTO);
        }
    }

    @Override
    public void workflowAddTrigger(FormDataAddDTO formDataAddDTO, BeforeSaveVO beforeSaveVO, FormDataAddVO formDataSaveVO) throws XbbException {
        workflowFormDataOperationService.workflowAddTrigger(formDataAddDTO, beforeSaveVO, formDataSaveVO);
    }
}
